package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.repository.mapper.OrderPasswordRecordMapper;
import com.haixiaoke.saas.repository.pojo.Order;
import com.haixiaoke.saas.repository.pojo.OrderPasswordRecord;
import com.haixiaoke.saas.repository.service.IOrderPasswordRecordService;
import com.haixiaoke.saas.repository.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订单临时密码授权次数记录表
 * 
 * @author panda
 * @date 2023-07-25
 */
@Service
public class OrderPasswordRecordServiceImpl implements IOrderPasswordRecordService {
    @Autowired
    private OrderPasswordRecordMapper orderPasswordRecordMapper;
    @Autowired
    private IOrderService iOrderService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public OrderPasswordRecord selectOrderPasswordRecordById(Long id) {
        return orderPasswordRecordMapper.selectOrderPasswordRecordById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param OrderPasswordRecord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OrderPasswordRecord> selectOrderPasswordRecordList(OrderPasswordRecord OrderPasswordRecord) {
        return orderPasswordRecordMapper.selectOrderPasswordRecordList(OrderPasswordRecord);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param OrderPasswordRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOrderPasswordRecord(OrderPasswordRecord OrderPasswordRecord) {
        return orderPasswordRecordMapper.insertOrderPasswordRecord(OrderPasswordRecord);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param OrderPasswordRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOrderPasswordRecord(OrderPasswordRecord OrderPasswordRecord) {
        return orderPasswordRecordMapper.updateOrderPasswordRecord(OrderPasswordRecord);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOrderPasswordRecordByIds(Long[] ids) {
        return orderPasswordRecordMapper.deleteOrderPasswordRecordByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteOrderPasswordRecordById(Long id) {
        return orderPasswordRecordMapper.deleteOrderPasswordRecordById(id);
    }

    @Override
    public int selectCountByOrderId(String orderId) {
        DateTime date = DateUtil.date();
        Date beginTime = DateUtil.beginOfMonth(date);
        Order order = iOrderService.selectOrderById(orderId);
        if (beginTime.before(order.getCheckInTime())) {
            beginTime = order.getCheckInTime();
        }
        return orderPasswordRecordMapper.selectCountByOrderId(orderId,beginTime, date);
    }
}
